/*
 * AddPatient.java
 *
 * Created on __DATE__, __TIME__
 */

package ui;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import net.sf.nachocalendar.CalendarFactory;
import net.sf.nachocalendar.components.DateField;
import javax.swing.JOptionPane;

import com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException;

import bean.ConnectionBean;

/**
 *
 * @author  __USER__
 */
public class AddPatient extends javax.swing.JInternalFrame
{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/** Creates new form AddPatient */
	public AddPatient()
	{
		initComponents();
	}
	
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents()
	{
		
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		idPatientName = new javax.swing.JTextField();
		idDateOfBirth = new DateField();
		idPhoneNumber = new javax.swing.JTextField();
		idResidancePlace = new javax.swing.JTextField();
		jScrollPane1 = new javax.swing.JScrollPane();
		idAddress = new javax.swing.JTextArea();
		idReset = new javax.swing.JButton();
		idAddPatient = new javax.swing.JButton();
		jLabel6 = new javax.swing.JLabel();
		idSex = new javax.swing.JComboBox();
		
		setClosable(true);
		setIconifiable(true);
		setMaximizable(true);
		setResizable(true);
		setTitle("Add New Patient");
		setAutoscrolls(true);
		setDoubleBuffered(true);
		setMaximumSize(new java.awt.Dimension(515, 460));
		setMinimumSize(new java.awt.Dimension(515, 460));
		setName("addPatient");
		setOpaque(true);
		setPreferredSize(new java.awt.Dimension(515, 460));
		setVisible(true);
		
		jLabel1.setText("Patient Name");
		
		jLabel2.setText("Date Of Birth");
		
		jLabel3.setText("Phone Number");
		
		jLabel4.setText("Residance Place");
		
		jLabel5.setText("Address");
		
		idPhoneNumber.addFocusListener(new java.awt.event.FocusAdapter()
		{
			public void focusLost(java.awt.event.FocusEvent evt)
			{
				idPhoneNumberFocusLost(evt);
			}
		});
		
		idAddress.setColumns(20);
		idAddress.setLineWrap(true);
		idAddress.setRows(5);
		idAddress.setWrapStyleWord(true);
		jScrollPane1.setViewportView(idAddress);
		
		idReset.setText("Reset");
		idReset.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent evt)
			{
				idResetActionPerformed(evt);
			}
		});
		
		idAddPatient.setText("Add Patient");
		idAddPatient.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent evt)
			{
				idAddPatientActionPerformed(evt);
			}
		});
		
		jLabel6.setText("Sex");
		
		idSex.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "M", "F" }));
		
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addGap(34, 34, 34).addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel1).addGroup(
								layout.createSequentialGroup().addGroup(
										layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
												layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(jLabel4,
														javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(
														jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE).addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(jLabel2,
														javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addComponent(
												jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
										layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addGroup(
												layout.createSequentialGroup().addComponent(idReset, javax.swing.GroupLayout.PREFERRED_SIZE, 83,
														javax.swing.GroupLayout.PREFERRED_SIZE).addGap(35, 35, 35).addComponent(idAddPatient,
														javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addComponent(
												idSex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(idDateOfBirth).addComponent(idPhoneNumber).addComponent(
												idResidancePlace).addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 289,
												Short.MAX_VALUE).addGroup(
												layout.createSequentialGroup().addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
														idPatientName))))).addContainerGap(64, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addGap(35, 35, 35).addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1).addComponent(idPatientName,
								javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(
						javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(idSex,
								javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
								jLabel6)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel2).addComponent(idDateOfBirth,
								javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(
						javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
								layout.createSequentialGroup().addComponent(jLabel3).addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel4).addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel5)).addGroup(
								layout.createSequentialGroup().addComponent(idPhoneNumber, javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(idResidancePlace,
										javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
										jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE))).addGap(50, 50, 50).addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(idReset).addComponent(idAddPatient)).addContainerGap(
						31, Short.MAX_VALUE)));
		
		pack();
	}// </editor-fold>
	//GEN-END:initComponents
	
	private void idPhoneNumberFocusLost(java.awt.event.FocusEvent evt)
	{
		String phoneNumber = idPhoneNumber.getText();
		if (phoneNumber.startsWith("+91")) idPhoneNumber.setText(idPhoneNumber.getText().substring(3));
		phoneNumber = idPhoneNumber.getText();
		if (phoneNumber.startsWith("0")) idPhoneNumber.setText(idPhoneNumber.getText().substring(1));
	}
	
	private void idAddPatientActionPerformed(java.awt.event.ActionEvent evt)
	{
		if (validateInput())
		{
			Connection con = ConnectionBean.getConnection();
			try
			{
				PreparedStatement psm = con.prepareStatement("insert into sameermehrotra.patient values (?, ?, ?, ?, ?, ?)");
				psm.setString(1, idPatientName.getText().trim());
				psm.setDate(2, new java.sql.Date(
						new SimpleDateFormat("dd-MM-yyyy").parse(idDateOfBirth.getFormattedTextField().getText().trim()).getTime()));
				psm.setString(3, idPhoneNumber.getText().trim());
				psm.setString(4, idAddress.getText().trim());
				psm.setString(5, idResidancePlace.getText().trim());
				psm.setString(6, idSex.getSelectedObjects()[0].toString());
				psm.executeUpdate();
				JOptionPane.showMessageDialog(this, "Patient " + idPatientName.getText() + " has been added successfully");
			}
			catch (MySQLIntegrityConstraintViolationException integrityConstraintViolationException)
			{
				JOptionPane.showMessageDialog(this, "Patient with this contact number already exist");
			}
			catch (Exception e)
			{
				JOptionPane.showMessageDialog(this, "An Error occured Please contact Rituraj with this screenshot = \n" + e.getMessage());
				e.printStackTrace();
			}
		}
		else
		{
			JOptionPane.showMessageDialog(this, "Please Fill All Fields");
		}
	}
	
	private boolean validateInput()
	{
		if (idPatientName.getText().trim().length() == 0 || idDateOfBirth.getFormattedTextField().getText().trim().length() == 0
				|| idPhoneNumber.getText().trim().length() == 0 || idAddress.getText().trim().length() == 0
				|| idResidancePlace.getText().trim().length() == 0) { return false; }
		return true;
	}
	
	private void idResetActionPerformed(java.awt.event.ActionEvent evt)
	{
		idPatientName.setText("");
		idPhoneNumber.setText("");
		idResidancePlace.setText("");
		idAddress.setText("");
		idSex.setSelectedIndex(0);
	}
	
	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton idAddPatient;
	private javax.swing.JTextArea idAddress;
	private  DateField idDateOfBirth;
	private javax.swing.JTextField idPatientName;
	private javax.swing.JTextField idPhoneNumber;
	private javax.swing.JButton idReset;
	private javax.swing.JTextField idResidancePlace;
	private javax.swing.JComboBox idSex;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JScrollPane jScrollPane1;
	// End of variables declaration//GEN-END:variables
	
}
